<%@page import="com.swim.entity.RegisteredUser"%>
<%@page import="com.swim.entity.SkillRequest"%>
<%@page import="com.swim.entity.CollaborationRequest"%>
<%@page import="com.swim.entity.FriendshipRequest"%>
<%@page import="com.swim.entity.Skill"%>
<%@page import="java.util.List"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>SWIM</title>
<link href='http://fonts.googleapis.com/css?family=Arvo'
	rel='stylesheet' type='text/css' />
<link href="css/style.css" rel="stylesheet" type="text/css"
	media="screen" />
<script type='text/javascript'>
	function accept(type, id, accept) {
		var form = document.createElement("form");
		form.setAttribute("method", "post");
		if (type == "newskill" && accept == "true")
			form.setAttribute("action", "newskill.jsp");
		else {
			form.setAttribute("action", "index.jsp");

			hiddenField = document.createElement("input");
			hiddenField.setAttribute("type", "hidden");
			hiddenField.setAttribute("name", "accept");
			hiddenField.setAttribute("value", accept);
			form.appendChild(hiddenField);

			hiddenField = document.createElement("input");
			hiddenField.setAttribute("type", "hidden");
			hiddenField.setAttribute("name", "type");
			hiddenField.setAttribute("value", type);
			form.appendChild(hiddenField);
		}

		hiddenField = document.createElement("input");
		hiddenField.setAttribute("type", "hidden");
		hiddenField.setAttribute("name", "id");
		hiddenField.setAttribute("value", id);
		form.appendChild(hiddenField);

		document.body.appendChild(form);
		form.submit();
	}
</script>
</head>
<body>
	<%
		Object admin = request.getSession().getAttribute("admin");
	%>
	<div id="menu-wrapper">
		<div id="menu">
			<ul>
				<li class="current_page_item"><a href="index.jsp">Homepage</a></li>
				<li><a href="collaboration.jsp">Collaborations</a></li>
				<li><a href="friends.jsp">Friends</a></li>
				<li><a href="edit.jsp">Edit Profile</a></li>
				<li><a href="logout.jsp">Logout</a></li>

				<%
					if (admin == null || !(Boolean) admin) {
				%>
				<li><a href="newskillreq.jsp">Request Skill</a></li>
				<%
					} else {
						%>	
						<li><a href="newskill.jsp">Create Skill</a></li>
						<%}%>
			</ul>
		</div>
		<!-- end #menu -->
	</div>

	<div id="wrapper">
		<div id="header-wrapper">
			<div id="header">
				<div id="logo">
					<h1>
						<a href="#">SWIM</a>
					</h1>
					<p>"A social network for friends needing or offering help"</p>
				</div>
			</div>
		</div>
		<!-- end #header -->
		<div id="page">
			<div id="page-bgtop">
				<div id="page-bgbtm">
					<div id="content"> 
						<div class="post">
							<h2 class="title">
								Welcome back,
								<%
								RegisteredUser user = (RegisteredUser) request.getAttribute("user");
								if (user != null) {
									out.append(user.getName());
								}
							%>!
							</h2>
							<div class="entry">
								<%
									List<RegisteredUser> notifs = (List<RegisteredUser>) request
											.getAttribute("notifications");
									if (notifs.size() > 0) {%>
									
								<p style="font-size: 17px;">Here are some news for you:</p>
								<%
										out.println("<ul>");
										for (RegisteredUser r : notifs) {
											out.println("<li><b>" + r.getName() + " "
													+ r.getSurname() + "</b>");
								%>
									accepted your friendship request! <a href="suggestions.jsp?via=<%=r.getEmail() %>">See some friend suggestions!</a>
								</li>
								<%
									}
										out.println("</ul>");
									} else {
										out.println("You don't have news.. :(");
									}
								%>
							</div>
						</div>
						<div class="post">

							<h2 class="title">Collaboration requests</h2>
							<div class="entry">

								<%
									List<CollaborationRequest> collreqs = (List<CollaborationRequest>) request
											.getAttribute("collabreq");
									if (collreqs.size() > 0) {
										out.println("<ul>");
										for (CollaborationRequest c : collreqs) {
											out.println("<li><b>" + c.getEmailFrom().getName() + " "
													+ c.getEmailFrom().getSurname() + "</b>");
								%>
								<ul
									style="list-style-type: none; display: inline; padding-right: 20px;">
									<li
										style="list-style-type: none; display: inline; padding-right: 30px;">
										<a
										href="javascript:accept('collaboration','<%=c.getId() + "','true'"%>);"><u>Accept</u></a>
									</li>
									<li
										style="list-style-type: none; display: inline; padding-right: 30px;">
										<a
										href="javascript:accept('collaboration','<%=c.getId() + "','false'"%>);"><u>Deny</u></a>
									</li>
								</ul>
								</li>
								<%
									}
										out.println("</ul>");
									} else {
										out.println("You don't have any collaboration requests :(");
									}
								%>
							</div>

						</div>
						<div style="clear: both;">&nbsp;</div>
					</div>
					
					<!-- end #content -->
					<div id="sidebar">
						<ul>
							<li>
								<h2>Search a skill</h2>
								<p>Search for users with a skill you need:</p>
								<div style="margin-left: 7%;">
									<form method="get" action="results.jsp">
										<div>
											<select name="query">
												<%
													Iterable<String> list = (Iterable<String>) request
															.getAttribute("skills");
													for (String s : list) {
												%><option><%=s%></option>
												<%
													}
												%>

											</select> <input type="hidden" name="type" value="skill" /> <input
												type="submit" value="Search" /> <br />
											<br />
										</div>
									</form>
								</div>
								<p>
									<a href="search.jsp"><i><u>Advanced search..</u></i></a>
								</p>
							</li>
							
							<%
								List<FriendshipRequest> reqs = (List<FriendshipRequest>) request
										.getAttribute("friendreq");
								if (reqs.size() > 0) {
							%>
							<li>
							<div>
								<h2>Friendship requests:</h2>
								<ul>

									<%
										for (FriendshipRequest s : reqs) {
												out.println("<li><b>" + s.getEmailFrom().getName() + " "
														+ s.getEmailFrom().getSurname() + "</b>");
									%>
									<br/>
									<ul
										style="list-style-type: none; display: inline; padding-right: 20px;">
										<li
											style="list-style-type: none; display: inline; padding-right: 30px;">
											<a
											href="javascript:accept('friendship','<%=s.getId() + "','true','" + s.isDirect() + "'"%>);"><u>Accept</u></a>
										</li>
										<li style="list-style-type: none; display: inline;"><a
											href="javascript:accept('friendship',''<%=s.getId() + "','false','" + s.isDirect() + "'"%>);"><u>Deny</u></a>
										</li>
									</ul>
									<%
										out.println("</li>");
									}
									%>
									</ul>
							</div>
							</li>
							<%
								}
							%>


						<%
							if (admin != null && (Boolean) admin) {
						%>
							<li>
							<div>
								<h2>New Skill requests:</h2>
								<ul>
									<%Iterable<SkillRequest> nsreq = (Iterable<SkillRequest>) request
											.getAttribute("skillRequests");
										for (SkillRequest s : nsreq) {
												out.println("<li><div><b>" + s.getEmailFrom().getName() + " "
														+ s.getEmailFrom().getSurname() + "</b>");
									%>
									<br />
									<ul
													style="list-style-type: none; display: inline; padding-right: 20px;">
													<li
														style="list-style-type: none; display: inline; padding-right: 30px;">
														<a
														href="javascript:accept('newskill','<%=s.getId() + "','true'"%>);"><u>Accept</u></a>
													</li>
													<li
														style="list-style-type: none; display: inline; padding-right: 30px;">
														<a
														href="javascript:accept('newskill','<%=s.getId() + "','false'"%>);"><u>Deny</u></a>
													</li>
									</ul>
									<%
										out.println("<div></li>");
									}
									%>
								</ul>
							</div>
							</li>
							<%
								}
							%>
						</ul>
					</div>
					<!-- end #sidebar -->
					<div style="clear: both;">&nbsp;</div>
				</div>
			</div>
		</div>
		<!-- end #page -->
	</div>
	<div id="footer">
		<p></p>
	</div>
	<!-- end #footer -->
</body>
</html>
